package cn.jingyuan.bee.utils.convert.impl;

import cn.jingyuan.bee.utils.TypeUtils;
import cn.jingyuan.bee.utils.convert.AbstractConverter;
import cn.jingyuan.bee.utils.convert.ConverterRegistry;

import java.lang.reflect.Type;
import java.util.concurrent.atomic.AtomicReference;

/**
 * {@link AtomicReference} 转换器
 */
public class AtomicReferenceConverter extends AbstractConverter<AtomicReference> {

    @Override
    protected AtomicReference<?> convertInternal(Object value) {
        // 尝试将值转换为 Reference 泛型的类型
        Object targetValue = null;
        final Type paramType = TypeUtils.getTypeArgument(AtomicReference.class);
        if (!TypeUtils.isUnknown(paramType)) {
            targetValue = ConverterRegistry.getInstance().convert(paramType, value);
        }
        if (null == targetValue) {
            targetValue = value;
        }

        return new AtomicReference<>(targetValue);
    }

}
